<?php
class ControllerModuleNewsletter extends Controller {
    protected function index($setting) {
        $this->language->load('module/newsletter');

        $this->data['template'] = 'catalog/view/theme/'.$this->config->get('config_template').'/';
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['action_register'] = $this->url->link('module/newsletter/register');
        $this->data['action_unregister'] = $this->url->link('module/newsletter/unregister');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/newsletter.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/newsletter.tpl';
        } else {
            $this->template = 'default/template/module/newsletter.tpl';
        }

        $this->render();
    }

    public function register() {
        $result = false;

        if(($this->request->server['REQUEST_METHOD'] == 'POST')){
            $data = array(
                'email' => $this->request->post['subscriber'],
            );

            $this->load->model('newsletter/subscriber');
            $result = $this->model_newsletter_subscriber->addSubscriber($data);
        }

        $this->response->setOutput($result);
    }

    public function unregister() {
        $result = false;

        if(($this->request->server['REQUEST_METHOD'] == 'POST')){
            $email = $this->request->post['subscriber'];

            $this->load->model('newsletter/subscriber');
            $result = $this->model_newsletter_subscriber->deleteByEmail($email);
        }

        $this->response->setOutput($result);
    }
}